<?php
namespace Admin\Frontend\Controllers;

use Helpers\Tool;
use Helpers\User;
use Phalcon\Mvc\Dispatcher;
use Helpers\Controller;
use Helpers\Main;

class ControllerBase extends Controller
{

    public function beforeExecuteRoute(Dispatcher $dispatcher)
    {
        //跳过不需验证控制器
        if (in_array(strtolower($dispatcher->getControllerName()), ['index', 'help'])) {
            return true;
        }

        //判断key是否正确，正确则判断对应操作
        $key = $this->request->get('key', 'string');
        if ($key && $key == strtoupper(md5(date('Ymd') . '+' . $this->config->get('key')))) {
            if (in_array(strtolower($dispatcher->getActionName()), [
                strtolower('modifyAppStoreRecord'),
                strtolower('getBoxList'),
                strtolower('releaseApplication'),
                strtolower('publishArticle'),
                strtolower('info'),
            ])) {
                return true;
            }
        }
        if (in_array(strtolower($dispatcher->getActionName()), [
            strtolower('saveFeedBack'),
            strtolower('getMobileInit'),
            strtolower('ajaxTest'),
            strtolower('getHistoryList'),
            strtolower('feedbackDetail'),
            strtolower('assessFeedback'),
            strtolower('generateAll'),
            strtolower('runAppPlanInstall'),
            strtolower('doSynCode')
        ])) {
            return true;
        }

        //检查是否已登陆，否则跳转到登陆页面
        if (!User::isLogin()) {
            Main::goLoginPage();
//            $this->dispatcher->forward([
//                'controller' => 'Index',
//                'action'     => 'login',
//            ]);
            return false;
        }

        //检查访问管理员专属页面时是否为管理员身份
        if (in_array($dispatcher->getControllerName(), ['sys'])
            && !in_array(Main::getLoginUserInfo('logonName'), ['admin', 'aichenk'])) {
            return $this->sendViewError('对不起，您没有查看此页面权限');
        }
        return true;
    }

    public function afterExecuteRoute(Dispatcher $dispatcher)
    {
        return $this->view->setVar('Ver', '?ver=' . VER);
    }

}